package org.abacus.platform.database.impl;

import java.util.ArrayList;
import java.util.List;

import org.abacus.platform.database.Column;
import org.abacus.platform.database.DataType;
import org.abacus.platform.database.Datasource;
import org.abacus.platform.database.Platform;
import org.abacus.platform.database.Table;
import org.abacus.platform.database.TableGroup;

public class DatabaseHelper {

	private DatabaseHelper() {
		super();
	}

	public static String create(Table arg0) {
		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("create table ");
		sb.append(arg0.getTableName());
		sb.append(" ( ");
		List<String> primaryKeys = new ArrayList<String>(1);
		for (Column c : arg0.getColumns()) {
			if (null == c) {
				continue;
			}
			sb.append(c.getColumnName());
			sb.append(' ');
			sb.append(c.getDataType());
			if (null != c.getDataLength()) {
				sb.append('(');
				sb.append(c.getDataLength());
				if (null != c.getDataScale()) {
					sb.append('.');
					sb.append(c.getDataScale());
				}
				sb.append(')');
			}
			if (c.getNullable()) {
				sb.append(" null ");
			} else {
				sb.append(" not null ");
			}
			if (c.getPrimaryKey()) {
				primaryKeys.add(c.getColumnName());
			}
			sb.append(',');
		}
		if (primaryKeys.size() > 0) {
			sb.append(" primary key (");
			for (String primaryKey : primaryKeys) {
				sb.append(primaryKey);
				sb.append(',');
			}
			sb.append(")");
		}
		int index = sb.lastIndexOf(",");
		if (-1 != index) {
			sb.deleteCharAt(index);
		}
		sb.append(" )");
		return sb.toString();
	}

	public static String alter(Table arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public static String rename(Table arg0, String nowname) {
		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("alter table ");
		sb.append(arg0.getTableName());
		sb.append(" rename to ");
		sb.append(nowname);
		return sb.toString();
	}

	public static String drop(Table arg0) {
		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("drop table ");
		sb.append(arg0.getTableName());
		return sb.toString();
	}

	public static String create(Column arg0) {
		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("alter table ");
		sb.append(arg0.getTable().getTableName());
		sb.append(" add column ");
		sb.append(arg0.getColumnName());
		sb.append(' ');
		sb.append(arg0.getDataType());
		if (null != arg0.getDataLength()) {
			sb.append('(');
			sb.append(arg0.getDataLength());
			if (null != arg0.getDataScale()) {
				sb.append('.');
				sb.append(arg0.getDataScale());
			}
			sb.append(')');
		}
		if (arg0.getNullable()) {
			sb.append(" null ");
		} else {
			sb.append(" not null ");
		}
		return sb.toString();
	}

	public static String alter(Column arg0) {
		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("alter table ");
		sb.append(arg0.getTable().getTableName());
		sb.append(" modify column ");
		sb.append(arg0.getColumnName());
		sb.append(' ');
		sb.append(arg0.getDataType());
		if (null != arg0.getDataLength()) {
			sb.append('(');
			sb.append(arg0.getDataLength());
			if (null != arg0.getDataScale()) {
				sb.append('.');
				sb.append(arg0.getDataScale());
			}
			sb.append(')');
		}
		if (arg0.getNullable()) {
			sb.append(" null ");
		} else {
			sb.append(" not null ");
		}
		return sb.toString();
	}

	public static String change(Column arg0, String nowname) {
		if (null == nowname) {
			return alter(arg0);
		}

		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("alter table ");
		sb.append(arg0.getTable().getTableName().toLowerCase());
		sb.append(" change column ");
		sb.append(arg0.getColumnName());
		sb.append(' ');
		sb.append(nowname);
		sb.append(' ');
		sb.append(arg0.getDataType());
		if (null != arg0.getDataLength()) {
			sb.append('(');
			sb.append(arg0.getDataLength());
			if (null != arg0.getDataScale()) {
				sb.append('.');
				sb.append(arg0.getDataScale());
			}
			sb.append(')');
		}
		if (arg0.getNullable()) {
			sb.append(" null ");
		} else {
			sb.append(" not null ");
		}
		return sb.toString();
	}

	public static String drop(Column arg0) {
		StringBuffer sb = new StringBuffer();
		if (null == arg0) {
			return sb.toString();
		}
		sb.append("alter table ");
		sb.append(arg0.getTable().getTableName());
		sb.append(" drop column ");
		sb.append(arg0.getColumnName());
		return sb.toString();
	}

	public static String create(TableGroup arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into table_group (id,group_name,parent_id) values(");

		if (null == arg0.getId()) {
			sb.append("null");
		} else {
			sb.append(arg0.getId());
		}
		sb.append(',');

		if (null == arg0.getGroupName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getGroupName());
			sb.append("'");
		}
		sb.append(',');
		if (null == arg0.getParent()) {
			sb.append("null");
		} else {
			sb.append(arg0.getParent().getId());
		}
		sb.append(')');
		return sb.toString();
	}

	public static String alter(TableGroup arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("update table_group set group_name=");

		if (null == arg0.getGroupName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getGroupName());
			sb.append("'");
		}
		sb.append(',');

		sb.append("parent_id=");
		if (null == arg0.getParent()) {
			sb.append("null");
		} else {
			sb.append(arg0.getParent().getId());
		}

		sb.append(" where id=");
		sb.append(arg0.getId());

		return sb.toString();
	}

	public static String drop(TableGroup arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("delete from table_group where id=");
		sb.append(arg0.getId());
		return sb.toString();
	}

	public static String create(Datasource arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into data_provider (id,column_name,filter_column,filter_column_value,primary_key_name,table_name) values(");

		if (null == arg0.getId()) {
			sb.append("null");
		} else {
			sb.append(arg0.getId());
		}
		sb.append(',');

		if (null == arg0.getColumnName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getColumnName());
			sb.append("'");
		}
		sb.append(',');

		if (null == arg0.getFilterColumn()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getFilterColumn());
			sb.append("'");
		}
		sb.append(',');

		if (null == arg0.getFilterColumnValue()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getFilterColumnValue());
			sb.append("'");
		}
		sb.append(',');

		if (null == arg0.getPrimaryKeyName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getPrimaryKeyName());
			sb.append("'");
		}
		sb.append(',');

		if (null == arg0.getTableName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getTableName());
			sb.append("'");
		}
		sb.append(')');
		return sb.toString();
	}

	public static String alter(Datasource arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("update data_provider set column_name=");

		if (null == arg0.getColumnName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getColumnName());
			sb.append("'");
		}
		sb.append(',');

		sb.append("filter_column=");
		if (null == arg0.getFilterColumn()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getFilterColumn());
			sb.append("'");
		}
		sb.append(',');

		sb.append("filter_column_value=");
		if (null == arg0.getFilterColumnValue()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getFilterColumnValue());
			sb.append("'");
		}
		sb.append(',');

		sb.append("primary_key_name=");
		if (null == arg0.getPrimaryKeyName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getPrimaryKeyName());
			sb.append("'");
		}
		sb.append(',');

		sb.append("table_name=");
		if (null == arg0.getTableName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getTableName());
			sb.append("'");
		}

		sb.append(" where id=");
		sb.append(arg0.getId());

		return sb.toString();
	}

	public static String drop(Datasource arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("delete from data_provider where id=");
		sb.append(arg0.getId());
		return sb.toString();
	}

	public static String create(Platform arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into platform (id,platform_code,platform_name) values(");

		if (null == arg0.getId()) {
			sb.append("null");
		} else {
			sb.append(arg0.getId());
		}
		sb.append(',');

		if (null == arg0.getPlatformCode()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getPlatformCode());
			sb.append("'");
		}
		sb.append(',');

		if (null == arg0.getPlatformName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getPlatformName());
			sb.append("'");
		}
		sb.append(')');
		return sb.toString();
	}

	public static String alter(Platform arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("update platform set platform_code=");

		if (null == arg0.getPlatformCode()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getPlatformCode());
			sb.append("'");
		}
		sb.append(',');

		sb.append("platform_name=");
		if (null == arg0.getPlatformName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getPlatformName());
			sb.append("'");
		}

		sb.append(" where id=");
		sb.append(arg0.getId());

		return sb.toString();
	}

	public static String drop(Platform arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("delete from platform where id=");
		sb.append(arg0.getId());
		return sb.toString();
	}

	public static String create(DataType arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("insert into data_type (id,data_type_name,platform_id) values(");

		if (null == arg0.getId()) {
			sb.append("null");
		} else {
			sb.append(arg0.getId());
		}
		sb.append(',');

		if (null == arg0.getDataTypeName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getDataTypeName());
			sb.append("'");
		}
		sb.append(',');
		if (null == arg0.getPlatform()) {
			sb.append("null");
		} else {
			sb.append(arg0.getPlatform().getId());
		}
		sb.append(')');
		return sb.toString();
	}

	public static String alter(DataType arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("update data_type set data_type_name=");

		if (null == arg0.getDataTypeName()) {
			sb.append("null");
		} else {
			sb.append("'");
			sb.append(arg0.getDataTypeName());
			sb.append("'");
		}
		sb.append(',');

		sb.append("platform_id=");
		if (null == arg0.getPlatform()) {
			sb.append("null");
		} else {
			sb.append(arg0.getPlatform().getId());
		}

		sb.append(" where id=");
		sb.append(arg0.getId());

		return sb.toString();
	}

	public static String drop(DataType arg0) {
		StringBuffer sb = new StringBuffer();
		sb.append("delete from data_type where id=");
		sb.append(arg0.getId());
		return sb.toString();
	}

}
